package com.service.account.impl;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.dao.account.AccountDAO;
import com.domain.account.AccountDO;
import com.service.account.AccountService;
import com.vo.account.Account;

public class AccountServiceImpl implements AccountService {
	@Autowired
	private AccountDAO accountDAO;

	public List getAllAccounts() {
		List<AccountDO> accountDOList = accountDAO.selectAllAccounts();
		List<Account> accountList = new ArrayList<Account>();
		if (accountDOList != null && accountDOList.size() > 0) {
			for (AccountDO accountDO : accountDOList) {
				Account account = new Account();
				// 通过BeanUtils来将DO拷贝到VO中
				try {
					BeanUtils.copyProperties(account, accountDO);
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					e.printStackTrace();
				}
				accountList.add(account);
			}
		}

		return accountList;
	}

	public int insertAccount(Account account) {
		AccountDO accountDO = new AccountDO();
		// 通过BeanUtils来将DO拷贝到VO中
		try {
			BeanUtils.copyProperties(accountDO, account);
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		accountDAO.insertAccount(accountDO);

		if (true)
			return 0;
		else
			return 1;

	}
}
